package Frame;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

import FCRendering.FCRBattleOrderConstant;
import Function.FCAnimationLibrary;
import Function.FCEventLibrary;
import Function.FCItemLibrary;
import VWorldGameStudio.FCItem;

/**
 * 编辑或新建物品，即售货机展示、物品栏显示的物品
 */
public class FCEditItem extends JDialog {

	private JFileChooser filec = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()) {
		protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
			JDialog jDialog = super.createDialog(parent);
			jDialog.setSize(500, 500);
			jDialog.setTitle("选择文件");
			jDialog.setResizable(false);
			jDialog.setLocationRelativeTo(null);
			return jDialog;
		};
	};

	private Vector<String> itemId = new Vector<String>();
	// length = 26
	private String[] name = { "攻击伤害", "伤害目标", "加血/血量", "回血目标", "回血持续", "加防/防甲", "防御目标", "防御持续", "物品减防",
			"减防目标", "减防持续", "攻击破甲", "破甲目标", "属性目标", "属性持续",  "属性数值", "致 命 率","容量/马力/弹仓","修理价格","补弹单价"
	,"升级价格"};
	private JLabel[] shuxingname = new JLabel[name.length];
	private JTextField[] shuxing = new JTextField[name.length];

	private String[] elementsEn = new String[]{"null", FCRBattleOrderConstant.S_Gas,FCRBattleOrderConstant.S_Fire,
			FCRBattleOrderConstant.S_Frozen,FCRBattleOrderConstant.S_Acid,FCRBattleOrderConstant.S_Paralysis, FCRBattleOrderConstant.S_Sleep};

	// length = 2
	private String catalog[] = { "character", "tank" };
	// length = 26
	private String[][] type = { { "food", "drug", "tool", "throw", "arm", "hat", "jacket", "pants", "shoes", "plotItem","artwork",
	"wantedPic","book"},
			{ "gun1", "gun2", "plate", "shell", "engine", "Cdevice", "chassis", "radar", "other","se"} };
	// length = 4
	private String[] occupation = { "all", "hunter", "machinist", "warrior" };

	public FCEditItem(boolean isnew, FCItemLibrary itl, FCEventLibrary evel, FCAnimationLibrary anil) {
		setTitle((isnew) ? "物品管理-添加物品" : "物品管理-编辑物品");
		setModal(true);
		setBounds(0, 0, 615, 700);
		setLayout(null);
		setLocationRelativeTo(null);
		setResizable(false);

		JLabel a = new JLabel("物品ID");
		JComboBox<String> aa = new JComboBox<String>();
		aa.setBounds(70, 10, 180, 20);
		if (isnew) {
			a = new JLabel("物品ID");
			a.setBounds(10, 10, 60, 20);
			this.add(a);
			aa.setEnabled(false);
			aa.addItem(String.valueOf(itl.getItemNewID()));
		} else {
			a = new JLabel("物品名称");
			a.setBounds(10, 10, 60, 20);
			this.add(a);
			aa.setEnabled(true);
			for (String itemName : itl.getItemName()) {
				aa.addItem(itemName);
			}
		}
		this.add(aa);

		JLabel b = new JLabel("物品名称");
		b.setBounds(260, 10, 60, 20);
		this.add(b);
		JTextField bb = new JTextField();
		bb.setBounds(320, 10, 180, 20);
		this.add(bb);

		JLabel zl = new JLabel("物品重量");
		zl.setBounds(500, 10, 60, 20);
		this.add(zl);
		JTextField zl1 = new JTextField("0");
		zl1.setBounds(560, 10, 40, 20);
		this.add(zl1);

		JLabel c = new JLabel("物品 目");
		c.setBounds(10, 40, 60, 20);
		this.add(c);

		JComboBox<String> ca = new JComboBox<String>();
		ca.setBounds(70, 40, 130, 20);
		ca.addItem("人物");
		ca.addItem("坦克");
		this.add(ca);

		JLabel cb = new JLabel("物品种类");
		cb.setBounds(210, 40, 60, 20);
		this.add(cb);
		JComboBox<String> cc = new JComboBox<String>();
		cc.setBounds(270, 40, 130, 20);
		setTypeItem(false, cc, zl, zl1);
		this.add(cc);

		JLabel cd = new JLabel("适用职业");
		cd.setBounds(410, 40, 60, 20);
		this.add(cd);

		JComboBox<String> ce = new JComboBox<String>();
		ce.setBounds(470, 40, 130, 20);
		ce.addItem("全部");
		ce.addItem("猎人");
		ce.addItem("机械师");
		ce.addItem("战士");
		this.add(ce);

		ca.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setTypeItem((ca.getSelectedItem().toString().equals("坦克")), cc, zl, zl1);
			}
		});

		for (int i = 0; i < name.length; i++) {
			shuxingname[i] = new JLabel(name[i]);
			shuxingname[i].setBounds(10 + 200 * (i % 3), 70 + 30 * (int) (i / 3), 60, 20);
			this.add(shuxingname[i]);
			shuxing[i] = new JTextField();
			shuxing[i].setBounds(70 + 200 * (i % 3), 70 + 30 * (int) (i / 3), 130, 20);
			shuxing[i].setText("0");
			this.add(shuxing[i]);
		}

		JLabel tips = new JLabel("Tips：例如，属性数值是指毒气属性的数值，当物品是防甲时就是毒气防御，当是枪时就是伤害，");
		tips.setBounds(10, 320, 600, 20);
		this.add(tips);
		JLabel tips2 = new JLabel("xx目标是指，作用的目标数量是多少个，xx持续是指作用多少回合，容量对于发动机则是马力");
		tips2.setBounds(10, 340, 600, 20);
		this.add(tips2);
		JLabel tips3 = new JLabel("主炮都是表示弹仓，‘加血/血量’有双重定义，有加血功能的就是加血，没的就是血量，类推");
		tips3.setBounds(10, 360, 600, 20);
		this.add(tips3);

		JLabel g = new JLabel("属性");
		g.setBounds(410, 390, 60, 20);
		this.add(g);

		JComboBox<String> ga = new JComboBox<String>();
		ga.setBounds(470, 390, 130, 20);
		ga.addItem("无");
		ga.addItem("毒气");
		ga.addItem("火炎");
		ga.addItem("冷冻");
		ga.addItem("酸");
		ga.addItem("麻痹");
		ga.addItem("睡眠");
		this.add(ga);

		JLabel target = new JLabel("升级目标");
		target.setBounds(10, 390, 120, 20);
		this.add(target);

		JComboBox<String> targetbox = new JComboBox<String>();
		targetbox.setBounds(130, 390, 130, 20);
		targetbox.addItem("null");
		for(String name: itl.getItemName()){
			targetbox.addItem(name);
		}
		this.add(targetbox);

		JLabel d = new JLabel();
		d.setBounds(520, 415, 32, 32);
		d.setBorder(BorderFactory.createLineBorder(Color.black));
		this.add(d);

		JLabel f = new JLabel("物品事件");
		f.setBounds(10, 420, 60, 20);
		this.add(f);

		JComboBox<String> fa = new JComboBox<String>();
		fa.setBounds(70, 420, 130, 20);
		fa.addItem("null");
		this.add(fa);

		JLabel fb = new JLabel("物品材质");
		fb.setBounds(210, 420, 60, 20);
		this.add(fb);

		JTextField fc = new JTextField();
		fc.setBounds(270, 420, 130, 20);
		fc.setEnabled(false);
		fc.setDisabledTextColor(Color.black);
		this.add(fc);

		JButton fd = new JButton("选择");
		fd.setBounds(410, 420, 70, 20);
		fd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (filec.showOpenDialog(null) == filec.APPROVE_OPTION)
					fc.setText(filec.getSelectedFile().getPath().replaceAll("\\\\", "/"));
				if (fc.getText() != null)
					d.setIcon(new ImageIcon(fc.getText()));
			}
		});
		this.add(fd);

		//加一个攻击动画
		JComboBox<String> animTipbox = new JComboBox<String>();
		animTipbox.setBounds(70, 460, 130, 20);
		//获取所有的动画id就行了
		animTipbox.addItem("null");
		for(String animationId: FCGMaster.getAnimationLibrary().getAnimationId()){
			animTipbox.addItem(animationId);
		}
		this.add(animTipbox);
		//加一个选择框
		JLabel animTip = new JLabel("攻击动画(只有武器类item加吧，可以不加)");
		animTip.setBounds(210, 460, 280, 20);
		this.add(animTip);

		//再加一个底盘编辑穴的
		JComboBox<String> xue1box = new JComboBox<String>();
		xue1box.setBounds(10, 500, 80, 20);
		xue1box.addItem("主炮穴关");
		xue1box.addItem("主炮穴开");
		this.add(xue1box);
		JComboBox<String> xue2box = new JComboBox<String>();
		xue2box.setBounds(100, 500, 80, 20);
		xue2box.addItem("副炮穴关");
		xue2box.addItem("副炮穴开");
		this.add(xue2box);
		JComboBox<String> xue3box = new JComboBox<String>();
		xue3box.setBounds(190, 500, 80, 20);
		xue3box.addItem("SE穴关");
		xue3box.addItem("SE穴开");
		this.add(xue3box);
		xue1box.setVisible(false);
		xue2box.setVisible(false);
		xue3box.setVisible(false);

		JComboBox<String> shellcasebox = new JComboBox<String>();
		shellcasebox.setBounds(280, 500, 100, 20);
		for(int i = 0; i< 9;i++){
			shellcasebox.addItem(i+"个弹仓");
		}
		this.add(shellcasebox);
		shellcasebox.setVisible(false);

		JLabel relateMonster = new JLabel("关联怪物");
		relateMonster.setBounds(250, 530, 80, 20);
		this.add(relateMonster);
		JComboBox<String> relateMonsterbox = new JComboBox<String>();
		relateMonsterbox.setBounds(330, 530, 150, 20);
		relateMonsterbox.addItem("null");
		for(String monsterName: FCGMaster.monsterLibrary.monstername){
			relateMonsterbox.addItem(monsterName);
		}
		this.add(relateMonsterbox);

		JLabel depictjl = new JLabel("次要描述LandId(参考ch.vw)");
		depictjl.setBounds(10, 530, 240, 20);
		this.add(depictjl);
		JTextArea depictText = new JTextArea();
		depictText.setBounds(10, 550, 590, 20);
		this.add(depictText);

		JButton ok = new JButton("确认添加");
		ok.setBounds(10, 650, 590, 18);
		this.add(ok);

		cc.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (cc.getItemCount() > 0)
					if (cc.getSelectedItem().toString().equals("投掷") || cc.getSelectedItem().toString().equals("武器")
							|| cc.getSelectedItem().toString().equals("主炮")
							|| cc.getSelectedItem().toString().equals("副炮")) {
						fa.removeAllItems();
						fa.addItem("null");
						for (int i = 0; i < anil.getAnimationNum(); i++)
							fa.addItem(anil.getIDOfNum(i));
					} else {
						fa.removeAllItems();
						fa.addItem("null");
						for (int i = 0; i < evel.getEventNum(); i++) {
							fa.addItem(evel.getEventNameOfID(i));
						}
					}
				if("底盘".equals(cc.getSelectedItem())) {
					xue1box.setVisible(true);
					xue2box.setVisible(true);
					xue3box.setVisible(true);
					shellcasebox.setVisible(true);
				}else {
					xue1box.setVisible(false);
					xue2box.setVisible(false);
					xue3box.setVisible(false);
					shellcasebox.setVisible(false);
				}
			}
		});

		aa.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				FCItem it;
				if(!isnew){
					it = itl.getItemOfID((String) aa.getSelectedItem());
					animTipbox.setSelectedItem(it.getAttackAnimation());
				}else {
					it = itl.getItemOfNum(aa.getSelectedIndex());
				}

				bb.setText(it.getName());
				ca.setSelectedIndex((it.getCatalog().equals("tank")) ? 1 : 0);
				cc.setSelectedIndex(getTypeItem(ca.getSelectedIndex(), it.getType()));
				//ce.setSelectedIndex(getOccupationItem(it.getOccupation()));
				shuxing[0].setText(String.valueOf(it.getAttackHurt()));
				shuxing[1].setText(String.valueOf(it.getAttackNum()));
				shuxing[2].setText(String.valueOf(it.getBloodAdd()));
				shuxing[3].setText(String.valueOf(it.getBloodNum()));
				shuxing[4].setText(String.valueOf(it.getBloodRound()));
				shuxing[5].setText(String.valueOf(it.getDefenseAdd()));
				shuxing[6].setText(String.valueOf(it.getDefenseNum()));
				shuxing[7].setText(String.valueOf(it.getDefernseRound()));
				shuxing[8].setText(String.valueOf(it.getSubdefense()));
				shuxing[9].setText(String.valueOf(it.getSubdefenseNum()));
				shuxing[10].setText(String.valueOf(it.getSubdefenseRound()));
				shuxing[11].setText(String.valueOf(it.getDesdefense()));
				shuxing[12].setText(String.valueOf(it.getDesdefenseNum()));
				shuxing[13].setText(String.valueOf(it.getElementsNum()));
				shuxing[14].setText(String.valueOf(it.getElementsRound()));
				shuxing[15].setText(String.valueOf(it.getElementsValue()));
				shuxing[16].setText(String.valueOf(it.getCrit()));
				shuxing[17].setText(String.valueOf(it.getCapacity()));
				shuxing[18].setText(String.valueOf(it.getRepairPrice()));
				shuxing[19].setText(String.valueOf(it.getFillServicePrice()));
				shuxing[20].setText(String.valueOf(it.getUpgradeMoney()));
				zl1.setText(String.valueOf(it.getWeight()));
				ga.setSelectedIndex(getElementsItem(it.getElements()));
				fc.setText(it.getPngUrl());
				targetbox.setSelectedItem(it.getUpgradeTarget());
				if (fc.getText() != null) {
					d.setIcon(new ImageIcon(fc.getText()));
				}
				xue1box.setSelectedIndex(it.getCannonHoles()[0]);
				xue2box.setSelectedIndex(it.getCannonHoles()[1]);
				xue3box.setSelectedIndex(it.getCannonHoles()[2]);
				shellcasebox.setSelectedIndex(it.getShellCaseNum());
				depictText.setText(it.getDepict());
				depictText.updateUI();
				relateMonsterbox.setSelectedItem(it.getRelatedMonster()==null?"null":it.getRelatedMonster());
			}
		});

//		cc.addActionListener(new ActionListener() {
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				switch(cc.getSelectedIndex()) {
//				case 0:
//					
//				}
//			}
//		});

		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (bb.getText() != null && !bb.getText().equals("")) {
					if (!itl.isItemRepeat(bb.getText()) || !isnew) {
						if (fc.getText() != null && !fc.getText().equals("")) {
							int id = 0;
							if(isnew){
								id = StringToInt(aa.getSelectedItem().toString());
							}else {
								id = itl.getIdByName(aa.getSelectedItem().toString());
							}
							int[] dipanxue = new int[]{xue1box.getSelectedIndex(), xue2box.getSelectedIndex(), xue3box.getSelectedIndex()};
							itl.registerItem(isnew, id, bb.getText(),
									catalog[ca.getSelectedIndex()], type[ca.getSelectedIndex()][cc.getSelectedIndex()],
									occupation[ce.getSelectedIndex()], StringToInt(shuxing[0].getText()),
									StringToInt(shuxing[1].getText()), StringToInt(shuxing[2].getText()),
									StringToInt(shuxing[3].getText()), StringToInt(shuxing[4].getText()),
									StringToInt(shuxing[5].getText()), StringToInt(shuxing[6].getText()),
									StringToInt(shuxing[7].getText()), StringToInt(shuxing[8].getText()),
									StringToInt(shuxing[9].getText()), StringToInt(shuxing[10].getText()),
									StringToInt(shuxing[11].getText()), StringToInt(shuxing[12].getText()),
									StringToInt(shuxing[13].getText()), StringToInt(shuxing[14].getText()),
									(String) targetbox.getSelectedItem(),
									StringToInt(shuxing[15].getText()), StringToDouble(shuxing[16].getText()),
									elementsEn[ga.getSelectedIndex()], StringToDouble(shuxing[17].getText()),
									(String) fa.getSelectedItem(), fc.getText(), Double.parseDouble(zl1.getText())
							,(String) animTipbox.getSelectedItem(), dipanxue, shellcasebox.getSelectedIndex(),
									StringToInt(shuxing[18].getText()),
									StringToInt(shuxing[19].getText()),	StringToInt(shuxing[20].getText()),
									depictText.getText(),(String)relateMonsterbox.getSelectedItem());
							new FCTip("物品编辑", "物品设置成功!");
							dispose();
						} else
							new FCTip("物品编辑", "物品材质不得为空!");
					} else
						new FCTip("物品编辑", "名称重复!");
				} else
					new FCTip("物品编辑", "物品名称不得为空!");
			}
		});

		setVisible(true);
	}

	/**
	 * 更新物品种类下拉选择框
	 * 
	 * @param isTank
	 * @param dd
	 * @param zl
	 * @param zl1
	 */
	private void setTypeItem(boolean isTank, JComboBox<String> dd, JLabel zl, JTextField zl1) {
		dd.removeAllItems();
		if (!isTank) {
			dd.addItem("食品");
			dd.addItem("药品");
			dd.addItem("工具");
			dd.addItem("投掷");
			dd.addItem("武器");
			dd.addItem("帽子");
			dd.addItem("上衣");
			dd.addItem("裤子");
			dd.addItem("鞋子");
			dd.addItem("其它");
			zl.setVisible(false);
			zl1.setVisible(false);
		} else {
			dd.addItem("主炮");
			dd.addItem("副炮");
			dd.addItem("装甲片");
			dd.addItem("弹药");
			dd.addItem("发动机");
			dd.addItem("C装置");
			dd.addItem("底盘");
			dd.addItem("雷达");
			dd.addItem("其它");
			dd.addItem("SE炮");
			zl.setVisible(true);
			zl1.setVisible(true);
		}
	}

	/**
	 * 获取物品种类选中
	 * 
	 * @param selectedCa
	 * @param itemname
	 * @return
	 */
	private int getTypeItem(int selectedCa, String itemname) {
		String[] a = type[selectedCa];
		for (int i = 0; i < a.length; i++) {
			if (itemname.equals(a[i])) {
				return i;
			}
		}
		return a.length - 1;
	}

	/**
	 * 获取职业类型选中
	 * 
	 * @param occ
	 * @return
	 */
	private int getOccupationItem(String occ) {
		for (int i = 0; i < occupation.length; i++) {
			if (occupation[i].equals(occ)) {
				return i;
			}
		}
		return 0;
	}

	/**
	 * 获取属性选中
	 * 
	 * @param ele
	 * @return
	 */
	private int getElementsItem(String ele) {
		for (int i = 0; i < elementsEn.length; i++) {
			if (elementsEn[i].equals(ele)) {
				return i;
			}
		}
		return 0;
	}

	/**
	 * 输入框String转int
	 * 
	 * @param str
	 * @return
	 */
	public int StringToInt(String str) {
		if (str != null && !str.equals("")) {
			return Integer.parseInt(str);
		} else {
			return 0;
		}
	}

	/**
	 * 输入框String转double
	 * 
	 * @param str
	 * @return
	 */
	public double StringToDouble(String str) {
		if (str != null && !str.equals("")) {
			return Double.valueOf(str);
		} else {
			return 0;
		}
	}
}
